<template>
  <div>
    <template>
      <el-tabs type="border-card">
        <el-tab-pane label="产品信息">
          <div class="chanpinbox">
            <div class="uplog">
              <div class="logtxt">上传log</div>
              <el-upload
                class="avatar-uploader"
                action="http://123.56.105.243:8080/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl"
                 class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
            <div class="state">
              <div>状态</div>
              <template>
                <el-radio v-model="radio" label="1">备选项</el-radio>
                <el-radio v-model="radio" label="2">备选项</el-radio>
              </template>
            </div>
            <div class="productName">
              <div>排序:</div>
              <div>
                <el-input
                  class="cinput"
                  v-model="ids"
                  placeholder="请选择序号"
                ></el-input>
              </div>
            </div>
            <div class="upPrice">
              <div>上传图片</div>
              <el-upload
                action="https://jsonplaceholder.typicode.com/posts/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </div>
            <div class="productName">
              <div>产品名称:</div>
              <el-input
                class="cinput"
                v-model="productName"
                placeholder="请输入产品名称"
              ></el-input>
            </div>
          </div>
          <div>
            <el-input
              class="inpw"
              v-model="describe"
              placeholder="请输入内容"
            ></el-input>
            <div>
              <el-button>默认按钮</el-button>
              <el-button type="primary" @click="subenter">提交</el-button>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="配置管理" name="second">
          <div>
            <template>
              <el-table :data="list" style="width: 100%">
                <el-table-column prop="id" label="序号" width="180">
                </el-table-column>
                <el-table-column prop="describe" label="logo路径" width="180">
                </el-table-column>
                <el-table-column prop="productName" label="产品名称">
                </el-table-column>
                <el-table-column label="图片路径">
                  <template slot-scope="scope">
                    <img
                      :src="'http://'+scope.row.imageUrl"
                      alt="用户头像"
                      width="120"
                      height="120"
                    />
                  </template>
                </el-table-column>
                <el-table-column prop="address" label="产品描述">
                </el-table-column>
                <el-table-column prop="address" label="操作">
                  <template slot-scope="scope">
                  <el-button size="mini" type="info">置顶</el-button>
                  <el-button size="mini" type="success" @click="selects(scope.row.id)">编辑</el-button>
                    <el-button size="mini" @click="open(scope.row.id)" type="danger">删除</el-button>
                    <el-checkbox @change="seleAll(scope.row.id)"  label=""></el-checkbox>
                  </template>
                </el-table-column>
              </el-table>
            </template>
            <el-button type="primary" @click="delAll">批量删除</el-button>
          </div>
          <el-pagination background layout="prev, pager, next" @current-change="pages" :current-page="packageNation.curpage" :page-size="packageNation.pasiz" :total="packageNation.total">
          </el-pagination>
        </el-tab-pane> </el-tabs
    >
  </template>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="活动名称" :label-width="formLabelWidth">
      <el-input v-model="form.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="活动区域" :label-width="formLabelWidth">
      <el-input v-model="form.region" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="enterss(selectid)">确定</el-button>

  </div>
  <!-- 删除 -->
</el-dialog>
  </div>
</template>
<style lang="scss" scoped>
.inpw {
  max-height: 220px;
}
.chanpinbox {
  display: flex;
}
.state {
  width: 200px;
  height: 200px;
  margin-left: 100px;
}
.upPrice {
  margin-left: 10px;
}
.cinput {
  width: 142px;
}
.uplog {
  width: 140px;
  height: 140px;
}
.productName {
  width: 260px;
  display: flex;
}
:deep .el-tabs--border-card > .el-tabs__content {
  line-height: 100px;
}
.avatar-uploader {
  width: 140px;
  height: 140px;
  border: 1px solid #000;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>


import { adduserss, userLists ,state,deleteuser} from "../../request/api/userlist";
export default {
  data() {
    return {
      imges:"",
      curpage:"",
      pasiz:"",
      dialogImageUrl: "",
      dialogVisible: false,
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      list: [],
      ids: "",
      imageUrl: "",
      tabPosition: "left",
      radio: "1",
      priceId:0,
      input: "",
      packageNation:{
        curpage:1,
        pasiz:6,
        total:0
      },
      selectid:"",
      input2: "",
      userLists: "",
      describe: "",
      productName: "",
        dialogTableVisible: false,
        dialogFormVisible: false,
        form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        formLabelWidth: '120px',
        dellAllid:[]
    };
  },
  methods: {
    seleAll(id){
      console.log("iddddd",id);
      let ress=this.dellAllid.findIndex(item=>{
        return item==id
      })
      console.log(ress,"删除");
      if(ress==-1){
        this.dellAllid.push(id)
        console.log(this.dellAllid,"添加");
      }else{
        this.dellAllid.splice(ress,1)
        console.log(this.dellAllid,"添加");
      }
      // console.log("批量删除",this.dellAllid);
    },
    async getList(){
      let res = await userLists("/admin/product",{
        "curpage": this.packageNation.curpage,
        "pageSize": this.packageNation.pasiz
      })
      this.list=res.data.data.records
      this.packageNation.total=res.data.data.total
      console.log("商品列表",res);
    },
    async state(id){
      let res = await state(`/admin/product/${id}`)
      if(res.data.code==200){
        this.$message({ type: 'success', message: '修改成功!' });
      }
      },
    // 批量删除
    async delAll(){
      let res = await this.axios({
        url: `http://123.56.105.243:8080/admin/product/batch`,
        method: 'delete',
        data:{ids:this.dellAllid}
      })
      if(res.data.code==200){
        this.$message({ type: 'success', message: '批量删除成功!' }); 
        this.getList()
      }
      // let res= await deleteuser("/admin/product/batch",{
      //  "ids":this.dellAllid
      // })
      console.log("批量删除",res);
    },
    async handleBatchDelete() {
      if (this.selectedIds.length === 0) {
        this.$message.warning("请先选择需要批量删除的数据");
        return;
      }
      this.$confirm("此操作将永久删除选中文件，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            let res = await this.axios({
              url: `http://123.56.105.243:8080/admin/process/batch`,
              method: "delete",
              data: { ids: this.selectedIds },
            });
            this.$message.success("删除成功！");
            this.getList(); // 刷新列表
            this.selectedIds = []; // 清空选中的项
          } catch (error) {
            console.error("批量删除数据失败：", error);
            this.$message.error("删除失败，请重试");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    open(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(
    async() => { 
      let res = await deleteuser(`/admin/product/${id}`)
      if(res.data.code==200){
        this.$message({ type: 'success', message: '删除成功!' });  
            this.getList();
      }
      }).catch(() => { this.$message({ type: 'info', message: '已取消删除' })})
    },
    async selects(id){
      console.log(id);
      this.selectid=id
      this.dialogFormVisible = true
      
    },
async enterss(){
  this.dialogFormVisible=false
  let res = await adduserss("/admin/product",{
    "describe": this.form.region,
    "id": this.selectid,
    "indexView": 0,
    "logoPictureId": 0,
    "pictureId": this.imges,
    "productName": this.form.name,
    "sort": 0
  })
  if(res.data.code==200){
    this.open2()
    this.getList();
  }else{
    this.open3()
  }
  
  },
  open2() {
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      },
      open3() {
        this.$message({
          message: '修改失败',
          
        });
      },
    pages(index){
      console.log("11111");
      this.packageNation.curpage = index;
      this.getList();
    },
    // 产品添加
    async subenter() {
      let res = await adduserss("/admin/product", {
        describe: this.describe,
        id: this.ids,
        indexView: 0,
        logoPictureId: 0,
        pictureId: 0,
        productName: this.productName,
        sort: 0,
      });
      console.log(res, "res");
      // console.log(res, this.productName);
      if(res.data.code==200){
        this.$message({
          message: '提交成功',
          type: 'success'
        }); 
      }
    },
    // 产品信息获取
    async getList() {
      let res = await userLists("/admin/product/page", {
        pageNum: this.packageNation.curpage,
        pageSize: this.packageNation.pasiz,
      });
this.packageNation.total=res.data.total
      this.list = res.data.rows;
      console.log(res.data.rows);
    },

    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log("ressasdadas",res);
      this.imges=res.data.id
     
      if(res.code==200){
        this.$message({
          message: '提交成功',
          type: 'success'
        }); 
      }
      this.priceId = res.id;
    },
    beforeAvatarUpload(file) {  
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted() {
    this.getList();
  },
};
</script>